<!--
 * @Author: HX
 * @Date: 2024-12-05 11:14:08
 * @LastEditors: yuanweijiang
 * @LastEditTime: 2025-05-09 14:15:38
-->
<template>
	<div>
		<div id="container" class="mapName"></div>
	</div>
</template>
<script setup lang="ts">
import mapLoader from '/public/assets/loader.js';
import { onMounted, getCurrentInstance } from 'vue';
import { useMapInfo } from '/@/stores/map';
import initIconfont from '/@/utils/getStyleSheets';
import { useMessage } from '/@/hooks/message';
const mapStore = useMapInfo();
import * as Native from '/public/native/main.js';
import axios from 'axios';
const instance = getCurrentInstance();
onMounted(() => {
	let container = document.getElementById('container');
	instance.appContext.config.globalProperties.qtCanvas = container;
	mapLoader({
		el: container,
	}).then(([SSmap, GlobalViewer]) => {
		//设置Native全局变量
		window.Native = Native;
		initIconfont.repairMouseDrag();
		// document.getElementById('container').style.cursor = 'default';
		window.viewer = GlobalViewer; //设置视图全局变量
		// Native.setAuthorize();
		Native.addTiandituImagery();
		addwmtsImagery();
	});
});
function addwmtsImagery() {
	let mapObj = JSON.parse(localStorage.getItem('mapInfo'));
	const { url, position } = mapObj;
	switch (position.dataSourceType) {
		case 2:
			let wmsURL = `VERSION=1.1.1&IgnoreGetMapUrl=1&IgnoreReportedLayerExtents=1&SmoothPixmapTransform=1&crs=${
				position.srs
			}&dpiMode=4&format=image/png&layers=${position.layerName}&styles=${position.styleName ? position.styleName : ''}&url=${url}`;
			console.log(wmsURL);
			window.viewer.scene.globe.addWmsImageryLayer(wmsURL);
			let camera = window.viewer.scene.mainCamera.cameraController();
			let rectangle = SSmap.Rectangle.fromDegrees(
				position.latLonBoxMinX - 0.0001,
				position.latLonBoxMinY - 0.0001,
				position.latLonBoxMaxX + 0.0001,
				position.latLonBoxMaxY + 0.0001
			);
			camera.flyToRectangle(rectangle, 2, 0, -88, 0);
			break;
		case 3:
			if (position.dataType === '3Dtiles') {
				Native.addTileset(url, position.layerkey);
			}
			if (position.dataType == 'Fbx/Glb') {
				Native.heatmapFn.addModelHeat({
					geojson: url,
					id: 'Fbx',
					longitude: 114.255838,
					latitude: 22.725976,
					height: 150,
				});
			}
			break;
		case 1:
			if (position.dataType === '地形') {
				axios
					.get(url)
					.then((res) => {
						let camera = window.viewer.scene.mainCamera.cameraController();
						let rectangle = SSmap.Rectangle.fromDegrees(
							res.data.bounds[0] - 0.0001,
							res.data.bounds[1] - 0.0001,
							res.data.bounds[2] + 0.0001,
							res.data.bounds[3] + 0.0001
						);
						camera.flyToRectangle(rectangle, 2, 0, -88, 0);
						Native.addTerrain(url, position.layerkey);
					})
					.catch((err) => {
						useMessage().error(err.response.data?.msg);
					});
			}
			if (position.dataType === '瓦片地图') {
				axios({
					url: url,
					method: 'get',
					headers: { layerKey: position.layerkey },
				}).then((res) => {
					Native.addUrlTemplateImageryProvider({
						url,
						key: position.layerkey,
					});
				});
			}
			break;
	}
}
</script>
<style lang="scss" scoped>
.mapName {
	width: 100%;
	height: 100%;
	position: absolute;
}
</style>
